// pages/news/news.js
const util = require('../../utils/util.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    mohuanUser: '',
    newList: [],
    typeId: 1, //消息类型
    pageIndex: 1,
    pageCount: 6,
    totalPage: 4, //消息中心总页码

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      userInfo: app.globalData.userInfo,
      mohuanUser: app.globalData.mohuanUser
    })
    if (that.data.pageIndex <= that.data.totalPage) {
      this.getNewList()
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  // 获取消息列表
  getNewList: function() {
    var that = this
    var params = {
      page: that.data.pageIndex,
      rows: that.data.pageCount
    }
    var mohuanUser = app.globalData.mohuanUser
    var token = mohuanUser["X-AUTH-TOKEN"]
    var url = util.baseUrl
    // debugger
    console.log(that.data.pageIndex)
    console.log(that.data.totalPage)
    wx.request({
      url: url + '/c2mwebservice/api/v2/notification/' + that.data.mohuanUser.memberId + '/' + that.data.typeId,
      method: 'post',
      header: {
        'content-type': 'application/json', // 默认值
        'X-AUTH-TOKEN': token,
      },
      data: params,
      success: (res) => {
        console.log(res.data)
        console.log('获取消息列表')
        // console.log(parseInt(res.data.returnData.totalCount / that.data.pageCount)+1)        
        var concatNewList = that.data.newList.concat(res.data.returnData.notifications)
        var totalPages = parseInt(res.data.returnData.totalCount / that.data.pageCount) + 1
        if (res.data.returnData.notifications.length != 0) {
          that.setData({
            newList: concatNewList,
            pageIndex: that.data.pageIndex + 1,
            totalPage: totalPages //当前页码的总数
          })
          console.log(concatNewList)
          console.log(that.data.pageIndex)
          wx.setStorage({
            key: 'newList',
            data: that.data.newList,
          })

        } else {
          that.setData({
            // newList: concatNewList,
            pageIndex: 1,
            // totalPage: res.data.returnData //当前页码的总也水
          })
        }

      }
    })




  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this 
      var newLists = wx.getStorageSync('newList')

  },
  // 跳转消息消息详情页
  goNewDec: function(e) {
    var that = this
    console.log(e)
    var newsId = e.currentTarget.dataset.newid
    var indexs = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/newsDec/newsDec?notificationId=' + newsId + '&index=' + indexs,
    })
    // 将当前的小红点隐藏
    // debugger
    that.data.newList[indexs].isRead = 'Y'
    console.log(that.data.newList)
    that.setData({
      newList: that.data.newList
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    console.log('onReachBottom')
    if (that.data.pageIndex <= that.data.totalPage) {
      this.getNewList()
    }

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})